package com.lianzai.camera.service.impl;

import com.lianzai.camera.dao.CameraInfoDao;
import com.lianzai.camera.model.CameraInfo;
import com.lianzai.camera.service.CameraService;
import com.lianzai.common.executor.LocalSvrSyncExecutor;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.mobile.service.CommunityInfoService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CameraServiceImpl implements CameraService {
	@Autowired
	private CameraInfoDao cameraInfoDao;

	@Autowired
	private CommunityInfoService communityService;

	public CameraInfo getCameraById(Long cameraId)
	{
		return cameraInfoDao.getCameraById(cameraId);

	}

	public CommunityInfoDO getCommunityByCameraId(Long cameraId)
	{
		return cameraInfoDao.getCommunityByCameraId(cameraId);
	}

	@Override
	public List<Map> queryAllCameraList(){
		return cameraInfoDao.queryAllCameraList();
	}

	public int save(CameraInfo cameraInfo)
	{
		int ret = cameraInfoDao.save(cameraInfo);

		//将相机信息同步到本地系统
		CommunityInfoDO community = communityService.get(cameraInfo.getCommunityId());
		LocalSvrSyncExecutor.addCamera(cameraInfo, community, this);

		return ret;
	}

	public int updateCamera(CameraInfo cameraInfo)
	{
		int ret = cameraInfoDao.updateCamera(cameraInfo);

		//将相机信息同步到本地系统
		CommunityInfoDO community = communityService.get(cameraInfo.getCommunityId());
		LocalSvrSyncExecutor.updateCamera(cameraInfo, community, this);

		return ret;
	}

    public int syncToLocalSystem(long cameraId) {
		CameraInfo cameraInfo = getCameraById(cameraId);
		if (cameraInfo == null) {
			return 0;
		}

	    CommunityInfoDO community = communityService.get(cameraInfo.getCommunityId());
		if (community == null) {
			return 0;
		}

	    LocalSvrSyncExecutor.syncCameraToLocalSystem(cameraInfo, community, this);

	    return 1;
    }

	public int updateSyncStatus(CameraInfo cameraInfo)
	{
		return cameraInfoDao.updateSyncStatus(cameraInfo);
	}

	public int delCamera(CameraInfo cameraInfo)
	{
		int ret = cameraInfoDao.delCamera(cameraInfo.getId());

		//将相机信息同步到本地系统
		CommunityInfoDO community = communityService.get(cameraInfo.getCommunityId());
		LocalSvrSyncExecutor.delCamera(cameraInfo, community);

		return ret;
	}
	@Override
	public int updateCameraStatus(long id,int status){
        Map params = new HashedMap();
		params.put("id",id);
		params.put("status",status);
		return cameraInfoDao.updateCameraStatus(params);
	}

	@Override
	public Map queryCameraInformationByIp(String ip){
		return cameraInfoDao.queryCameraInformationByIp(ip);
	};

	@Override
	public List<CameraInfo> list(Map<String, Object> map){
		return cameraInfoDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return cameraInfoDao.count(map);
	}
}
